//
//  ZallPlaceholderTextViewCell.m
//  ZJS
//
//  Created by so on 16/9/1.
//  Copyright © 2016年 zall. All rights reserved.
//

#import "ZallPlaceholderTextViewCell.h"

@interface ZallPlaceholderTextView ()

/**
 *  @brief  占位符视图
 */
@property (strong, nonatomic, readonly) UITextView *placeholderView;

@end 

@implementation ZallPlaceholderTextView
@synthesize placeholderView = _placeholderView;

#pragma mark - life cycle
/**
 *  @brief  内存释放
 */
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
}

/**
 *  @brief  初始化
 */
- (instancetype)init {
    return ([self initWithFrame:CGRectZero]);
}

/**
 *  @brief  初始化
 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self addSubview:self.placeholderView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidBeginEditingNotification:) name:UITextViewTextDidBeginEditingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextViewTextDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditingNotification:) name:UITextViewTextDidEndEditingNotification object:nil];
    }
    return (self);
}
#pragma mark -

#pragma mark - override
/**
 *  @brief  重新布局
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    self.placeholderView.width = self.width;
}
#pragma mark -

#pragma mark - setter
- (void)setPlaceholder:(NSString *)placeholder {
    self.placeholderView.text = placeholder;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    self.placeholderView.textColor = placeholderColor;
}

- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder {
    self.placeholderView.attributedText = attributedPlaceholder;
}

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    self.placeholderView.font = font;
}
#pragma mark -

#pragma mark - getter
- (NSString *)placeholder {
    return ([self.placeholderView text]);
}

- (UIColor *)placeholderColor {
    return ([self.placeholderView textColor]);
}

- (NSAttributedString *)attributedPlaceholder {
    return ([self.placeholderView attributedText]);
}

- (UITextView *)placeholderView {
    if(!_placeholderView) {
        _placeholderView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40.0f)];
        _placeholderView.backgroundColor = [UIColor clearColor];
        _placeholderView.textColor = [UIColor lightGrayColor];
        _placeholderView.font = self.font;
        _placeholderView.editable = NO;
        _placeholderView.userInteractionEnabled = NO;
    }
    return (_placeholderView);
}
#pragma mark -

#pragma mark - public
- (void)prepareForReuse {
    self.placeholder = nil;
    self.attributedPlaceholder = nil;
}
#pragma mark -

#pragma mark - private
/**
 *  @brief  文本开始编辑
 */
- (void)textDidBeginEditingNotification:(NSNotification *)notification {
    if(notification.object != self) {
        return;
    }
    self.placeholderView.hidden = YES;
}

/**
 *  @brief  文本发生变化
 */
- (void)textDidChangeNotification:(NSNotification *)notification {
    if(notification.object != self) {
        return;
    }
    NSString *currentString = self.text;
    if([currentString length] > self.maxWordCount) {
        self.text = [currentString substringToIndex:self.maxWordCount];
    }
    self.placeholderView.hidden = ((self.text && [self.text length] > 0) || (self.attributedText && [self.attributedText length] > 0));
}

/**
 *  @brief  文本结束编辑
 */
- (void)textDidEndEditingNotification:(NSNotification *)notification {
    if(notification.object != self) {
        return;
    }
    self.placeholderView.hidden = ((self.text && [self.text length] > 0) || (self.attributedText && [self.attributedText length] > 0));
}
#pragma mark -

@end


/**
 *  @brief  大小的keypath
 */
static NSString * const ZJSContentSizeKeyPath = @"contentSize";

@interface ZallPlaceholderTextViewCell ()
/**
 *  @brief  文本占用大小
 */
@property (assign, nonatomic) CGSize zallContentSize;
@end

@implementation ZallPlaceholderTextViewCell
@synthesize textView = _textView;

#pragma mark - life cycle
/**
 *  @brief  内存释放
 */
- (void)dealloc {
    [_textView removeObserver:self forKeyPath:ZJSContentSizeKeyPath];
}

/**
 *  @brief  初始化
 */
- (instancetype)init {
    return ([self initWithFrame:CGRectZero]);
}

/**
 *  @brief  初始化
 */
- (instancetype)initWithFrame:(CGRect)frame {
    ZallPlaceholderTextViewCell *cell = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.frame = frame;
    return (cell);
}

/**
 *  @brief  初始化
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        _zallContentSize = self.bounds.size;
        [self.contentView addSubview:self.textView];
        [self.textView addObserver:self forKeyPath:ZJSContentSizeKeyPath options:NSKeyValueObservingOptionOld context:nil];
    }
    return (self);
}
#pragma mark -

#pragma mark - override
/**
 *  @brief  重新布局
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    self.textView.frame = UIEdgeInsetsInsetRect(self.contentView.bounds, self.contentInsets);
}

/**
 *  @brief  复用前调用
 */
- (void)prepareForReuse {
    [super prepareForReuse];
    [self.textView prepareForReuse];
}
#pragma mark -

#pragma mark - setter
- (void)setZallContentSize:(CGSize)zallContentSize {
    BOOL same = ABS(_zallContentSize.height == zallContentSize.height);
    if(same) {
        return;
    }
    [self willChangeValueForKey:@"zallContentSize"];
    _zallContentSize = zallContentSize;
    [self didChangeValueForKey:@"zallContentSize"];
    if(self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextViewCell:contentSizeDidChanged:)]) {
        [self.delegate placeholderTextViewCell:self contentSizeDidChanged:zallContentSize];
    }
}
#pragma mark -

#pragma mark - getter
- (CGSize)contentSize {
    return (_zallContentSize);
}

- (ZallPlaceholderTextView *)textView {
    if(!_textView) {
        _textView = [[ZallPlaceholderTextView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.contentView.bounds, self.contentInsets)];
        _textView.contentSize = _textView.bounds.size;
    }
    return (_textView);
}
#pragma mark -

#pragma mark - KVO
/**
 *  @brief  文本占用大小变化的观察者响应
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if(self.textView == object && (keyPath && [keyPath isEqualToString:ZJSContentSizeKeyPath]) && self.textView.text) {
        CGSize textContentSize = [self.textView contentSize];
        self.zallContentSize = CGSizeMake(textContentSize.width + self.contentInsets.left + self.contentInsets.right, textContentSize.height + self.contentInsets.top + self.contentInsets.bottom);
        return;
    }
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
#pragma mark -

@end
